const fillZoon = function (vel) {
  return vel < 10 ? "0" + vel : vel;
};

module.exports = function (date, format = "YYYY-MM-DD hh:mm:ss") {
  let DATE = null;
  //时间戳
  if (
    typeof date === "number" ||
    (typeof date === "string" && Number(date) == date)
  ) {
    date = Number(date);
    if (date.toString().length < 13) date *= 1000;
    DATE = new Date(date);
  } else if (typeof date === "string") {
    // 兼容ios系统的时间转换
    const value = date.replace(/-/g, "/");
    DATE = new Date(value);
  } else {
    return date;
  }
  let time = {
    YYYY: DATE.getFullYear(),
    YY: DATE.getFullYear().toString().substring(2),
    "M+": fillZoon(DATE.getMonth() + 1),
    "D+": fillZoon(DATE.getDate()),
    "h+": fillZoon(DATE.getHours()),
    "m+": fillZoon(DATE.getMinutes()),
    "s+": fillZoon(DATE.getSeconds()),
  };

  Object.keys(time).forEach((k) => {
    const reg = new RegExp(`${k}`, "g");
    format = format.replace(reg, time[k]);
  });
  return format;
};
